var app = angular.module('app');

app.directive('timeValidate', [function () {
    return {
        require: 'ngModel',
        link: function (scope, element, attrs, ngModel) {
            ngModel.$parsers.push(function (timeString) {
                if (!timeString) {
                    ngModel.$setValidity('timeValidate', false);
                    return;
                }
                var timeArray = timeString.split(':');
                if (timeArray.length != 2) {
                    ngModel.$setValidity('timeValidate', false);
                    return;
                }
                var hour = parseInt(timeArray[0]);
                if (hour < 0 || hour > 24) {
                    ngModel.$setValidity('timeValidate', false);
                    return;
                }
                var minute = parseInt(timeArray[1]);
                if (minute < 0 || minute > 60) {
                    ngModel.$setValidity('timeValidate', false);
                    return;
                }
                ngModel.$setValidity('timeValidate', true);
                return value;
            });
        }
    };
}]);